package com.cheetah.start.module.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.module.system.domain.SysMenu;
import com.cheetah.start.module.system.mapper.SysMenuMapper;
import com.cheetah.start.module.system.domain.SysRoleMenu;
import com.cheetah.start.module.system.mapper.SysRoleMenuMapper;
import com.cheetah.start.module.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统角色菜单服务
 *
 * @author Feel
 * @date 2021/09/05
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {


    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
    @Autowired
    private SysMenuMapper menuMapper;


    @Override
    public List<SysMenu> getMenuByRoleId(Integer roleId) {
        List<SysMenu> allMenu = menuMapper.selectList(null);
        List<SysMenu> menuByRoleId = roleMenuMapper.getMenuByRoleId(roleId);
        for (SysMenu menu : allMenu) {
            for (SysMenu sysMenu : menuByRoleId) {
                if (menu.getMenuId().equals(sysMenu.getMenuId())) {
                    menu.setCheckArr("1");
                }
            }
        }
        return allMenu;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Integer roleId, String[] menuIds) {
        removeByRoleId(roleId);
        List<SysRoleMenu> roleMenus = new ArrayList<>();
        for (String menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(Integer.parseInt(menuId));
            roleMenus.add(roleMenu);
        }
        return saveBatch(roleMenus);
    }


    @Override
    public boolean removeByRoleId(Integer roleId) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = getQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        return remove(queryWrapper);
    }

    @Override
    public boolean removeByMenuId(Integer menuId) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = getQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getMenuId, menuId);
        return remove(queryWrapper);
    }

    @Override
    public List<String> getPermissionByAdminId(Integer adminId) {
        return roleMenuMapper.getPermissionByAdminId(adminId);
    }

    @Override
    public List<SysRoleMenu> getListByRoleId(Integer roleId) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = getQueryWrapper();
        queryWrapper.eq(SysRoleMenu::getRoleId, roleId);
        return list(queryWrapper);
    }

    private LambdaQueryWrapper<SysRoleMenu> getQueryWrapper() {
        return Wrappers.lambdaQuery(SysRoleMenu.class);
    }
}
